﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.WebControls;
using VPLink2013.Framework.Extensions;
using VPLink2014.Data.MesApplis;

namespace VPLink2014
{
    public class ApplicationOperations : LayoutsPageBase
    {
        protected TeamFactory _teamFactory;
        public ApplicationOperations()
        {
            _teamFactory = new TeamFactory();
        }

        protected string InitFilterDefaultApplications(string search)
        {
            string responseJson = "{}";
            if (search != "application")
            {
                responseJson = _teamFactory.GetAllTeams(search).ToJson();
            }
            else
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString["filterType"])
                                            && !string.IsNullOrEmpty(this.Request.QueryString["filterValue"]))
                {
                    string filterType = this.Request.QueryString["filterType"];
                    string filterValue = this.Request.QueryString["filterValue"];

                    if (filterType == "directionRecursive")
                    {
                        VODirectionMapper direction = new VODirectionMapper(filterValue);
                        if (direction.direction != null)
                        {
                            responseJson = VPLinkLogCatalogue.GetDirectionRecursiveApplications(direction).ToJson();
                        }
                        else
                        {
                            responseJson = VPLinkLogCatalogue.GetAllOrphanApplications().ToJson();
                        }
                    }
                    else
                    {
                        VOTeamMapper team = _teamFactory.CreateTeam(filterType, filterValue);
                        if (team != null)
                        {
                            responseJson = team.GetApplicationsTeam().ToJson();
                        }
                    }
                }
                else
                {
                    responseJson = _teamFactory.GetAllOrphanApplications().ToJson();
                }
            }
            return responseJson;
        }

        protected string SetApplicationsToTeam(int nbAppli, string teamType, string teamName)
        {
            List<VOApplication> applications = new List<VOApplication>();
            try
            {
                for (int i = 0; i < nbAppli; i++)
                {
                    string rang = this.Request.QueryString["keyApplication[" + i + "][rang]"];
                    string id = this.Request.QueryString["keyApplication[" + i + "][id]"];
                    string obligatoire = this.Request.QueryString["keyApplication[" + i + "][obligatoire]"];
                    applications.Add(new VOApplication()
                    {
                        Id = Int32.Parse(id),
                        Rang = Int32.Parse(rang),
                        Obligatoire = obligatoire.ToBoolean()
                    });
                }
                if (teamType != "user")
                {
                    VOTeamMapper team = _teamFactory.CreateTeam(teamType, teamName);
                    if (team != null)
                    {
                        team.SetApplications(applications);
                    }
                    return team.ToJson();
                }
                else
                {
                    VOUserMapper user = new VOUserMapper(teamName);
                    user.SetApplications(applications);
                    return user.ToJson();
                }
            }
            catch (Exception e)
            {
                return string.Format(@"{""error"" : {0}}", e.Message.ToJson());
            }
        }
    }
}
